﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DRAGONBALL2BLUE
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //FileStream fs = new FileStream("大乐透.txt", FileMode.Create);

            ////清空缓冲区、关闭流
            //fs.Flush();
            //fs.Close();
        }

        static int NUMS = 8888;

        private void button2_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button2.Enabled = false;
            FileStream fs = new FileStream("DRAGONBALL_2blue.txt", FileMode.Append);
            //获得字节数组
            String ls_rec;
            if (label9.Text == "0")
            {
                ls_rec = "<-----------------历史存档" + System.DateTime.Now + "---------------\r\n"
                  + "\r\n" +
                   "<-----------------本次选号" + System.DateTime.Now + "---------------\r\n" +
                   "大乐透 \r\n"+
                  label1.Text + " " + label2.Text + " " + label3.Text + " " + label4.Text + " " + label5.Text + " -- " + label6.Text + " " + label7.Text + "\r\n";
            }
            else
            {
                ls_rec = label1.Text + " " + label2.Text + " " + label3.Text + " " + label4.Text + " " + label5.Text + " -- " + label6.Text + " " + label7.Text + "\r\n";
            }

            byte[] data = System.Text.Encoding.Default.GetBytes(ls_rec);
            //开始写入
            fs.Write(data, 0, data.Length);
            //清空缓冲区、关闭流
            fs.Flush();
            fs.Close();
            label9.Text = Convert.ToString(Convert.ToInt32(label9.Text) + 1);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button1.Enabled = true;

        }

        int GetRandomSeed() 
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //button1.Enabled = false;
            //button2.Enabled = true;
            for (int i = 0; i < 5; i++)
            {
                getballs();
                checkballs();
                regetball();
            }
        }


        public void getballs()
        {
            this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
            ArrayList balls = new ArrayList();
            int[] get6balls = new int[5];
            //Random ball = new Random(GetRandomSeed());
            int getball;
            for (int ii = 1; ii <= 5; ii++)
            {
                aa: balls.Clear();
                for (int bnum = 1; bnum <= NUMS; bnum++)
                {
                    getball = new Random(GetRandomSeed()).Next(0, 35) + 1;
                    balls.Add(getball);
                }
                getball = Convert.ToInt16(balls[new Random(GetRandomSeed()).Next(0, NUMS)]);
                if (ii == 1)
                {
                    get6balls[0] = getball;
                }
                else
                {
                    for (int iii = 0; iii < ii; iii++)
                    {
                        if (getball == get6balls[iii]) goto aa;
                    }
                    get6balls[ii - 1] = getball;
                }
            }


            for (int i = 0; i < get6balls.Length - 1; i++)
            {
                for (int j = 0; j < get6balls.Length - i - 1; j++)
                {
                    if (get6balls[j] < get6balls[j + 1])
                    {
                        int empty = get6balls[j];
                        get6balls[j] = get6balls[j + 1];
                        get6balls[j + 1] = empty;
                    }
                }
            }




            label1.Text = Convert.ToString(get6balls[4]);
            label2.Text = Convert.ToString(get6balls[3]);
            label3.Text = Convert.ToString(get6balls[2]);
            label4.Text = Convert.ToString(get6balls[1]);
            label5.Text = Convert.ToString(get6balls[0]);
            balls.Clear();

            int[] get2balls = new int[2];
            for (int ii = 1; ii <= 2; ii++)
            {
                bb: balls.Clear();
                for (int bnum = 1; bnum <= NUMS; bnum++)
                {
                    getball = new Random(GetRandomSeed()).Next(0, 12) + 1;
                    balls.Add(getball);
                }
                getball = Convert.ToInt16(balls[new Random(GetRandomSeed()).Next(0, NUMS)]);
                if (ii == 1)
                {
                    get2balls[0] = getball;
                }
                else
                {
                    for (int iii = 0; iii < ii; iii++)
                    {
                        if (getball == get2balls[iii]) goto bb;
                    }
                    get2balls[ii - 1] = getball;
                }
            }


            for (int i = 0; i < get2balls.Length - 1; i++)
            {
                for (int j = 0; j < get2balls.Length - i - 1; j++)
                {
                    if (get2balls[j] < get2balls[j + 1])
                    {
                        int empty = get2balls[j];
                        get2balls[j] = get2balls[j + 1];
                        get2balls[j + 1] = empty;
                    }
                }
            }

            label6.Text = Convert.ToString(get2balls[1]);
            label7.Text = Convert.ToString(get2balls[0]);
            balls.Clear();


            if (label1.Text.Length == 1)
            {
                label1.Text = "0" + label1.Text;
            }
            if (label2.Text.Length == 1)
            {
                label2.Text = "0" + label2.Text;
            }
            if (label3.Text.Length == 1)
            {
                label3.Text = "0" + label3.Text;
            }
            if (label4.Text.Length == 1)
            {
                label4.Text = "0" + label4.Text;
            }
            if (label5.Text.Length == 1)
            {
                label5.Text = "0" + label5.Text;
            }
            if (label6.Text.Length == 1)
            {
                label6.Text = "0" + label6.Text;
            }
            if (label7.Text.Length == 1)
            {
                label7.Text = "0" + label7.Text;
            }
            this.Cursor = System.Windows.Forms.Cursors.Default;
            button2.Enabled = true;
        }


        public void checkballs()
        {
            button1.Enabled = false;
            button2.Enabled = false;
            FileStream fs = new FileStream("DRAGONBALL_2blue.txt", FileMode.Append);
            //获得字节数组
            String ls_rec;
            if (label9.Text == "0")
            {
                ls_rec = "<-----------------历史存档" + System.DateTime.Now + "---------------\r\n"
                  + "\r\n" +
                   "<-----------------本次选号" + System.DateTime.Now + "---------------\r\n" +
                   "大乐透 \r\n" +
                  label1.Text + " " + label2.Text + " " + label3.Text + " " + label4.Text + " " + label5.Text + " -- " + label6.Text + " " + label7.Text + "\r\n";
            }
            else
            {
                ls_rec = label1.Text + " " + label2.Text + " " + label3.Text + " " + label4.Text + " " + label5.Text + " -- " + label6.Text + " " + label7.Text + "\r\n";
            }

            byte[] data = System.Text.Encoding.Default.GetBytes(ls_rec);
            //开始写入
            fs.Write(data, 0, data.Length);
            //清空缓冲区、关闭流
            fs.Flush();
            fs.Close();
            label9.Text = Convert.ToString(Convert.ToInt32(label9.Text) + 1);
        }


        public void regetball()
        {
            button1.Enabled = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("notepad.exe", "DRAGONBALL_2blue.txt");
            }
            catch(Exception e1)
            {
                MessageBox.Show("打开文件失败");
            }
        }


        public void checkballs(String str)
        {
            button1.Enabled = false;
            button2.Enabled = false;
            FileStream fs = new FileStream(DateTime.Now.ToString("yyyyMMdd")+".txt", FileMode.Append);
            //获得字节数组
            String ls_rec;
            if (label9.Text == "0")
            {
                ls_rec = str + "\r\n";
                    //"<-----------------历史存档" + System.DateTime.Now + "---------------\r\n"
                    //+ "\r\n" +
                    // "<-----------------本次选号" + System.DateTime.Now + "---------------\r\n" +
                    // "大乐透 \r\n" +
                    //label1.Text + " " + label2.Text + " " + label3.Text + " " + label4.Text + " " + label5.Text + " -- " + label6.Text + " " + label7.Text + "\r\n";
            }
            else
            {
                ls_rec = str + "\r\n";//label1.Text + " " + label2.Text + " " + label3.Text + " " + label4.Text + " " + label5.Text + " -- " + label6.Text + " " + label7.Text + "\r\n";
            }

            byte[] data = System.Text.Encoding.Default.GetBytes(ls_rec);
            //开始写入
            fs.Write(data, 0, data.Length);
            //清空缓冲区、关闭流
            fs.Flush();
            fs.Close();
            label9.Text = Convert.ToString(Convert.ToInt32(label9.Text) + 1);
        }

        public void getballs(int nums)
        {
            this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
            for (int fornum = 0; fornum < nums; fornum++)
            {
                String ballResult = "";
                ArrayList balls = new ArrayList();
                int[] get6balls = new int[5];
                //Random ball = new Random(GetRandomSeed());
                int getball;
                for (int ii = 1; ii <= 5; ii++)
                {
                aa: balls.Clear();
                    for (int bnum = 1; bnum <= NUMS; bnum++)
                    {
                        getball = new Random(GetRandomSeed()).Next(0, 35) + 1;
                        balls.Add(getball);
                    }
                    getball = Convert.ToInt16(balls[new Random(GetRandomSeed()).Next(0, NUMS)]);
                    if (ii == 1)
                    {
                        get6balls[0] = getball;
                    }
                    else
                    {
                        for (int iii = 0; iii < ii; iii++)
                        {
                            if (getball == get6balls[iii]) goto aa;
                        }
                        get6balls[ii - 1] = getball;
                    }
                }


                for (int i = 0; i < get6balls.Length - 1; i++)
                {
                    for (int j = 0; j < get6balls.Length - i - 1; j++)
                    {
                        if (get6balls[j] < get6balls[j + 1])
                        {
                            int empty = get6balls[j];
                            get6balls[j] = get6balls[j + 1];
                            get6balls[j + 1] = empty;
                        }
                    }
                }


                if (get6balls[4].ToString().Length == 1)
                {
                    ballResult = "0" + get6balls[4].ToString();
                }
                else 
                {
                    ballResult = get6balls[4].ToString();
                }

                if (get6balls[3].ToString().Length == 1)
                {
                    ballResult += " 0" + get6balls[3].ToString();
                }
                else
                {
                    ballResult += " " + get6balls[3].ToString();
                }

                if (get6balls[2].ToString().Length == 1)
                {
                    ballResult += " 0" + get6balls[2].ToString();
                }
                else
                {
                    ballResult += " " + get6balls[2].ToString();
                }

                if (get6balls[1].ToString().Length == 1)
                {
                    ballResult += " 0" + get6balls[1].ToString();
                }
                else
                {
                    ballResult += " " + get6balls[1].ToString();
                }

                if (get6balls[0].ToString().Length == 1)
                {
                    ballResult += " 0" + get6balls[0].ToString();
                }
                else
                {
                    ballResult += " " + get6balls[0].ToString();
                }

                balls.Clear();

                int[] get2balls = new int[2];
                for (int ii = 1; ii <= 2; ii++)
                {
                bb: balls.Clear();
                    for (int bnum = 1; bnum <= NUMS; bnum++)
                    {
                        getball = new Random(GetRandomSeed()).Next(0, 12) + 1;
                        balls.Add(getball);
                    }
                    getball = Convert.ToInt16(balls[new Random(GetRandomSeed()).Next(0, NUMS)]);
                    if (ii == 1)
                    {
                        get2balls[0] = getball;
                    }
                    else
                    {
                        for (int iii = 0; iii < ii; iii++)
                        {
                            if (getball == get2balls[iii]) goto bb;
                        }
                        get2balls[ii - 1] = getball;
                    }
                }


                for (int i = 0; i < get2balls.Length - 1; i++)
                {
                    for (int j = 0; j < get2balls.Length - i - 1; j++)
                    {
                        if (get2balls[j] < get2balls[j + 1])
                        {
                            int empty = get2balls[j];
                            get2balls[j] = get2balls[j + 1];
                            get2balls[j + 1] = empty;
                        }
                    }
                }

                if (get2balls[1].ToString().Length == 1)
                {
                    ballResult += " 0" + get2balls[1].ToString();
                }
                else
                {
                    ballResult += " " + get2balls[1].ToString();
                }

                if (get2balls[0].ToString().Length == 1)
                {
                    ballResult += " 0" + get2balls[0].ToString();
                }
                else
                {
                    ballResult += " " + get2balls[0].ToString();
                }
                balls.Clear();


               
                checkballs(ballResult);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            getballs(50*10000);
            MessageBox.Show("生成成功");
        }
    }
}
